﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Kardex.Modify
{
    public class PresenterModifyKardexView
    {
        IModifyBasicView viewModifyBasic;
        Model.Kardex modifyKardex = new Model.Kardex() { Person = new Model.Person() };
        IModifyKardexProductView view;
        public PresenterModifyKardexView(IModifyKardexProductView View, Model.Kardex Kardex)
        {
            view = View;
            modifyKardex = Kardex;
        }


        public void GoStepTwo()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyKardex = view.ModifyBasic.KardexToModify;
            if (!PassFirstStep())
                view.CallFirstPage();
            else
                view.CallSecondPage();
            ProcedureAfterCall();
        }

        public void GoStepOne()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyKardex = view.ModifyBasic.KardexToModify;
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void ModifyKardex()
        {
            view.ModifyBasic.ChargeDataToBasic();
            modifyKardex = view.ModifyBasic.KardexToModify;
            if (!PassFirstStep())
            {
                view.CallFirstPage();
                ProcedureAfterCall();
            }
            else if (!PassSecondStep())
            {
                view.CallSecondPage();
                ProcedureAfterCall();
            }
            else
            {
                ManagerKardex.UpdateKardex(modifyKardex);
                view.SystemMessage("El Kardex Fue Actualizado Exitosamente.!!!!");
                view.ReturnSearchKardex();
            }
        }

        private bool PassSecondStep()
        {
            try
            {
                Model.Kardex mp = new Model.Kardex();                
                mp.Person = view.ModifyBasic.KardexToModify.Person;
                ManagerKardex.VerifyDataPersonIsCorrect(mp);

                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message,"Actualizar"));
                return false;
            }
        }

        private bool PassFirstStep()
        {

            try
            {
                Model.Kardex mp = new Model.Kardex();
                mp.KardexId = this.modifyKardex.KardexId;
                mp.Allotment = view.ModifyBasic.KardexToModify.Allotment;
                ManagerKardex.VerifyDataPersonIsKardexWithoutPerson(mp);
                return true;
            }
            catch (Exception ex)
            {
                view.ErrorMessage(string.Format(ex.Message,"Actualizar"));
                return false;
            }
        }

        public void GoBackPage()
        {
            viewModifyBasic.GoBackPage();
        }

        public void GoNextPage()
        {
            viewModifyBasic.GoNextPage();
        }
        private void ProcedureAfterCall()
        {

            if (viewModifyBasic != null)
                modifyKardex = viewModifyBasic.KardexToModify;
            viewModifyBasic = view.ModifyBasic;
            viewModifyBasic.KardexToModify = modifyKardex;
            viewModifyBasic.Update();
        }

        public void FirstTime()
        {
            view.CallFirstPage();
            ProcedureAfterCall();
        }

        public void NotifyErrorMessage(string MessageValue)
        {
            view.ErrorMessage(MessageValue);
        }
        public void NotifySystemMessage(string MessageValue)
        {
            view.SystemMessage(MessageValue);
        }
    }
}
